/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.server.companypart;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.Collection;
import org.toools.gwt.wmc.client.companypart.CompanyPartService;
import org.toools.gwt.wmc.server.dao.ICompanyPartDAO;
import org.toools.gwt.wmc.jpa.CompanyPart;
import org.toools.gwt.wmc.server.WMCSession;

/**
 *
 * @author manuel
 */
public class CompanyPartServiceImpl extends RemoteServiceServlet implements CompanyPartService {

    private ICompanyPartDAO companypartdao;

    /** CompanyPart DAO **/
    public void setCompanypartdao( ICompanyPartDAO dao ) {
        this.companypartdao = dao;
    }
    public ICompanyPartDAO getCompanypartdao() {
		return companypartdao;
	}

    public CompanyPart getCompanyPartByID(Integer id) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || companypartdao.belongsTo( id, WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return companypartdao.getCompanyPartByID(id);

        return null;
    }

    public Collection<CompanyPart> getCompanyParts() {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
            return companypartdao.getCompanyParts();

        return companypartdao.getCompanyParts( WMCSession.getAccountID( getThreadLocalRequest() ) );
    }

    public CompanyPart create(CompanyPart cp) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
            return companypartdao.create(cp);
        //check if belongs to account
        else
            return companypartdao.create(cp);
    }

    public Boolean delete(CompanyPart cp) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || companypartdao.belongsTo( cp.getId(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return companypartdao.delete(cp);
        return false;
    }

    public Boolean update(CompanyPart cp) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || companypartdao.belongsTo( cp.getId(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return companypartdao.update(cp);
        return false;
    }

}
